function connectivitySummary
% FUNCTION CONNECTIVITYSUMMARY reads experimentsummary.xlsx and calculates
% connectivity rate and its statistics.
% this is the script for connectivity analysis. To dos: histgram of
% connected and unconnected, mean distance, std, comparison between cell
% types. 

% extract distance-connection data.
sp2sp = xlsread('C:\Data\Taro\CELLS\experimentsummary.xlsx','cellpairs','A5:F359');
sp2st = xlsread('C:\Data\Taro\CELLS\experimentsummary.xlsx','cellpairs','A371:F479');
st2sp = xlsread('C:\Data\Taro\CELLS\experimentsummary.xlsx','cellpairs','A491:F605');
st2st = xlsread('C:\Data\Taro\CELLS\experimentsummary.xlsx','cellpairs','A613:F756');

Connectivity(1) = connectivityAnalysis(sp2sp);
title('Sp->Sp')
Connectivity(2) = connectivityAnalysis(sp2st);
title('Sp->St')
Connectivity(3) = connectivityAnalysis(st2sp);
title('St->Sp')
Connectivity(4) = connectivityAnalysis(st2st);
title('St->St')
% plot connectivity rate
figure;
connectRate = [Connectivity(1).connectRate,Connectivity(2).connectRate,...
    Connectivity(3).connectRate,Connectivity(4).connectRate];

for conType = 1:length(Connectivity)
    [phat,pci] = binofit(Connectivity(conType).numConnect,...
        Connectivity(conType).numConnect + Connectivity(conType).numUnconnect);
    low95(conType) = pci(1);
    upper95(conType) = pci(2);
    int95{conType} = pci;
end

ConnectedPerAll = num2str([Connectivity.numConnect;[Connectivity.numUnconnect]...
    + [Connectivity.numConnect]]');
for k = 1:size(ConnectedPerAll)
    [tok, rem] = strtok(ConnectedPerAll(k,:));
    ConnectedPerAllStr{k} = [tok,' /',rem];
end
    
celltype = {['CSp > CSp\newline',ConnectedPerAllStr{1}],['CSp > CSt\newline',ConnectedPerAllStr{2}],...
    ['CSt > CSp\newline',ConnectedPerAllStr{3}],['CSt > CSt\newline',ConnectedPerAllStr{4}]};

colorMat = [0 0 1; 1 1 1;0 1 0; 1 0 0];

% plot connectivity and 95 % confidence interval.
ezbarweb(connectRate, connectRate-low95, connectRate-upper95, colorMat)
% set(gca,'XTickLabel',celltype,'interpreter','tex')
xticklabel_rotate([1:4],0,celltype,'interpreter','tex','HorizontalAlignment','center','VerticalAlignment','cap')
% title('Connectivity rate and 95 % confidence interval')
ylabel('Connectivity rate')

% save a .fig file.
saveas(gcf,'C:\Data\Taro\ANALYSIS\multi_rec_project\connectivity\connectivityRate.fig')

% export a .eps version.
editFigExport(gcf,'C:\Data\Taro\ANALYSIS\multi_rec_project\connectivity\connectivityRate.eps',5)

% chisquare. statistics of connectivity rate. cf. bonferroni method.
Pval = zeros(length(Connectivity));

P = [];
for m = 1:length(Connectivity) - 1;
    for n = m + 1:length(Connectivity);        
        Ob = [Connectivity(m).numConnect, Connectivity(n).numConnect;...
            Connectivity(m).numUnconnect, Connectivity(n).numUnconnect];
        if  6 < min(min(Ob))
            Pval(m,n) = chisquarecont(Ob); %p value
        else
            [Ppos,Pneg,Pboth] = FisherexTest(Connectivity(m).numConnect,...
                Connectivity(m).numUnconnect, Connectivity(n).numConnect,...
                Connectivity(n).numUnconnect);
            Pval(m,n) = Pneg;
        end
        P = [P,Pval(m,n)];
    end
end

% For a critical value, use Benjamini-Hochberg Method.
[Psort, Pindex] = sort(P);
Cm = [1:length(P)];
Cm = 1./Cm;
Cm = sum(Cm);
for i = 1:length(Psort)
    l(i) = .05*i/(length(Psort) * Cm);
end

R = max(find(Psort - l < 0));
T = Psort(R); % BH rejection hypothesis.
RejectH = Pindex(find(Psort - l < 0))
RejectHmat = (Pval <= T)
Pval